
export class Bezier {
    // n阶级贝塞尔
    static evaluate(t:number,points:mw.Vector[]):mw.Vector{
        let n = points.length - 1
        let result = mw.Vector.zero
        for (let i = 0; i < points.length; i++) {
            let f = Math.pow(1-t,n-i) * Math.pow(t,i) * this.combination(n,i)
            let d = points[i].clone().multiply(f)
            result = result.add(d)
        }
        return result
    }

    // 二阶贝塞尔
    static evaluate_2l(a:mw.Vector,b:mw.Vector,c:mw.Vector,t:number):mw.Vector{
        return mw.Vector.lerp(mw.Vector.lerp(a,b,t),mw.Vector.lerp(b,c,t),Math.max(0,Math.min(1,t)))
    }

    // 组合
    private static combination(max:number,cur:number):number{
        return this.factorial(max) / (this.factorial(max-cur) * this.factorial(cur))
    }

    // 阶乘
    private static factorial(x:number):number{
        let result = 1
        for (let i = 1; i <= x; i++) {
            result *= i
        }
        return result
    }
}